package com.dfcd.commonlib.retrofit;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;
import com.dfcd.commonlib.retrofit.dialog.ProgressCancelListener;
import com.dfcd.commonlib.retrofit.dialog.ProgressDialogHandler;
import com.dfcd.commonlib.retrofit.dialog.ResponseListener;
import com.dfcd.commonlib.utils.MLog;
import com.dfcd.commonlib.utils.NetUtils;
import com.google.gson.JsonSyntaxException;
import rx.Subscriber;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

/**
 * 自定义BaseSubscriber 统一处理请求
 *
 * @author zengyi
 * create at 2017/1/10 9:38
 */
public abstract class BaseSubscriber<T> extends Subscriber<T> implements ProgressCancelListener,
		ResponseListener {

	public static final String TAG = "BaseSubscriber";

	private Context mContext;
	private boolean isShow;
	private ProgressDialogHandler mProgressDialogHandler;


	public BaseSubscriber(Context context, boolean show) {
		this.mContext = context;
		this.isShow = show;
		mProgressDialogHandler = new ProgressDialogHandler(mContext,
				this, true);

	}

	private void showProgressDialog() {
		if (mProgressDialogHandler != null) {
			mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
		}
	}

	private void dismissProgressDialog() {
		if (mProgressDialogHandler != null) {
			mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
			mProgressDialogHandler = null;
		}
	}


	@Override
	public void onError(Throwable e) {
		try {
			//错误处理
			if (e instanceof TimeoutException || e instanceof SocketTimeoutException
					|| e instanceof ConnectException) {
				MLog.e(TAG, "请求超时");

			} else if (e instanceof JsonSyntaxException) {
				//假如导致这个异常触发的原因是服务器的问题，那么应该让服务器知道，所以可以在这里
				//选择上传原始异常描述信息给服务器
				MLog.e(TAG, "JSON解析错误");

			} else if (e instanceof HttpTimeException) {
				if (!TextUtils.isEmpty(e.getMessage())) {
					Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
				}

			} else {
				if (!TextUtils.isEmpty(e.getMessage())) {
					MLog.e("BaseSubscriber", e.getMessage());
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			dismissProgressDialog();
			if (!TextUtils.isEmpty(e.getMessage())) {
				MLog.e("BaseSubscriber", e.getMessage());
			}

		}


	}

	@Override
	public void onStart() {
		super.onStart();
		try {
			if (isShow) {
				showProgressDialog();
			}
			if (!NetUtils.isNetworkConnected(mContext)) {
				onCompleted();
				noNetWorkError();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	@Override
	public void onCompleted() {
		try {
			dismissProgressDialog();
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	@Override
	public void noNetWorkError() {

	}

	@Override
	public void onCancelProgress() {
		try {
			if (!isUnsubscribed()) {
				unsubscribe();
				dismissProgressDialog();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}